<template>
    <el-dialog ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">

            <el-row :gutter="15">
                <el-col :span="12">
                    <el-form-item label="关联鱼塘" prop="pondName">
                        <el-select v-model="ruleForm.pondName" filterable clearable class="w100p"
                            @change="handleChangeFishPond">
                            <el-option v-for="(item, index) in fishPondList" :key="index" :label="item.pondName"
                                :value="item.pondName" />
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="选择批次" prop="batchName">
                        <el-input placeholder="请输入" v-model="ruleForm.batchName"></el-input>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="选择品种" prop="varietyName">
                        <el-input placeholder="请输入" v-model="ruleForm.varietyName"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="品种图片">
                <UpLoadImage :limit="1" v-if="dialogVisible" @getFileIdList="(e) => { getFileIdList(e, 'pictureId') }"
                    :idList="pictureList">
                </UpLoadImage>
            </el-form-item>
            <el-form-item label="二维码图片">
                <UpLoadImage :limit="1" v-if="dialogVisible" @getFileIdList="(e) => { getFileIdList(e, 'fileld') }"
                    :idList="fileIdList">
                </UpLoadImage>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" v-show="!ruleForm.inputPondId" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import UpLoadImage from "@/components-production-management/UpLoadImage";
import traceabilityQRCode from '@/api/quality-management/traceabilityQRCode.js';
export default {
    components: { UpLoadImage },
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        },
        fishPondList: {
            type: Array,
            default: () => {
                return []
            }
        }
    },
    data() {
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            fileIdList: [],
            pictureList: [],
            rules: {
                batchName: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                varietyName: [
                    { required: true, message: '请输入', trigger: 'change' }
                ],
                pondName: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
            }
        }
    },
    computed: {},
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = traceabilityQRCode.add
                this.addEditName = '添加';
                this.fileIdList = []
                 this.pictureList=[]
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = traceabilityQRCode.edit
                this.addEditName = '编辑'
                this.fileIdList = this.data.fileld ? [this.data.fileld] : []
                this.pictureList = this.data.pictureId ? [this.data.pictureId] : []
                this.$set(this.ruleForm, 'fileld', this.data.fileld);
                this.$set(this.ruleForm, 'pictureId', this.data.pictureId);
            }
        },
        getFileIdList(data, key) {
            if (data.fileIdList.length) {
                this.$set(this.ruleForm, key, data.fileIdList[0])
            } else {
                this.$set(this.ruleForm, key, '')
            }
        },
        //选择鱼塘时提示用户是否存在该关联批次pondName
        async handleChangeFishPond(pondName) {
            let rut = this.fishPondList.find(item => item.pondName == pondName);
            this.$set(this.ruleForm, 'batchName', rut.vproductbatch)
            this.$set(this.ruleForm, 'varietyName', rut.cmaterialoname);
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm }).then(res => {
                        let msg = res.success ? '操作成功' : '操作失败'
                        this.$message[res.success ? 'success' : 'warning'](res.message || msg)
                        res.success && this.cancel();
                    }).catch(err => {
                        this.$message.error(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>